package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Concest;

public class Script extends NokaTag{
	private static final long serialVersionUID = 4329826108746878099L;
	
	private String src = null;//
	private String type = "text/javascript";
	private String charset=Concest.ENCODING;

	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		try {
			if(null==request.getAttribute(src)){
				StringBuffer sb = new StringBuffer();
				if(src.startsWith("/"))
					sb.append("<script type=\""+type+"\" src=\""+basepath(request)+src+(src.indexOf("?")==-1?"?":"&")+"v="+Concest.SYSTEM_START_TIME.getTime()+"\" charset=\""+charset+"\"></script>");
				else if(src.startsWith("http"))
					sb.append("<script type=\""+type+"\" src=\""+src+(src.indexOf("?")==-1?"?":"&")+"v="+Concest.SYSTEM_START_TIME.getTime()+"\" charset=\""+charset+"\"></script>");
				else
					sb.append("<script type=\""+type+"\" src=\""+basepath(request)+"/"+src+(src.indexOf("?")==-1?"?":"&")+"v="+Concest.SYSTEM_START_TIME.getTime()+"\" charset=\""+charset+"\"></script>");
				jspOut.write(sb.toString());
				request.setAttribute(src,"true");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	public String getSrc() {
		return src;
	}
	public void setSrc(String src) {
		this.src = src;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}
}
